import { createRouter, createWebHistory } from "vue-router"; // createWebHashHistory, createWebHistory

// /* Layout */
// const Layout = ():RouteComponent => import('@/layout/index.vue');

// /* Router Modules */
// import componentsRouter from './modules/components';
// import chartsRouter from './modules/charts';
// import nestedRouter from './modules/nested';
// import tableRouter from './modules/table';
import NotFound from "@/views/error/404.vue";

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 *
 * 注意：hidden、alwaysShow 属性配置移动到了meta中！！！
 */
export const constantRoutes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/sm/:id",
    component: () => import("@/views/sm/index.vue"),
    children: [
      {
        path: "sm2",
        component: () => import("@/views/sm/sm2.vue"),
      },
      {
        path: "sm4",
        component: () => import("@/views/sm/sm4.vue"),
      },
    ],
  },
  {
    path: "/index",
    component: () => import("@/views/dashboard/index.vue"),

    meta: { hidden: true },
  },
  {
    path: "/test",
    name: "test",
    component: () => import("@/views/test/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/system/users/:id",
    component: () => import("@/views/system/user/index.vue"),
    children: [
      {
        path: "/index",
        component: () => import("@/views/system/user/post/index.vue"),
      },
      {
        path: "/index",
        component: () => import("@/views/system/user/profile/index.vue"),
      },
    ],
    meta: { hidden: true },
  },
  { path: "/:pathMatch(.*)*", name: "NotFound", component: NotFound },
  // 将匹配以 `/user-` 开头的所有内容，并将其放在 `route.params.afterUser` 下
  { path: "/user-:afterUser(.*)", component: NotFound },
];

console.log("BASE_URL=", import.meta.env);

const createTheRouter = () =>
  createRouter({
    // history: createWebHashHistory(import.meta.env.BASE_URL),
    // 注意，如果要配置 HTML5 模式，则需要修改nginx配置，参考资料：
    // https://router.vuejs.org/zh/guide/essentials/history-mode.html
    history: createWebHistory(import.meta.env.BASE_URL),
    scrollBehavior: () => ({ top: 0 }),
    routes: constantRoutes,
  });

/*
创建路由
 */
const router = createTheRouter();

//全局注册 router
export function setupRouter(app) {
  app.use(router);
}

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createTheRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
